﻿using System.Security.Cryptography;
using System.Text;

namespace AuthenticationDemo.Common
{
    public static class EncryptionUtils
    {
        private const string iv = "2024abc12uidgh00";//固定16位數
        private const string secretKey = "8F2FDD792E4BF84AC722BA14E94CA0D8";//固定32位數

        public static string EncryptAES(this string content)
        {
            if (content == null || content.Length <= 0)
                throw new ArgumentNullException(nameof(content));
            if (secretKey == null || secretKey.Length != 32) // AES密钥长度通常为256位（32字节）  
                throw new ArgumentException("Key must be exactly 32 bytes long.", nameof(secretKey));
            if (iv == null || iv.Length != 16) // AES的块大小是128位（16字节）  
                throw new ArgumentException("IV must be exactly 16 bytes long.", nameof(iv));

            byte[] keyArray = Encoding.UTF8.GetBytes(secretKey);
            byte[] ivArray = Encoding.UTF8.GetBytes(iv);
            byte[] toEncryptArray = Encoding.UTF8.GetBytes(content);

            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = keyArray;
                aesAlg.IV = ivArray;

                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        csEncrypt.Write(toEncryptArray, 0, toEncryptArray.Length);
                        csEncrypt.Close();
                    }

                    byte[] encryptedData = msEncrypt.ToArray();

                    return Convert.ToBase64String(encryptedData);
                }
            }
        }

        public static string DecryptAES(this string content)
        {
            byte[] keyArray = Encoding.UTF8.GetBytes(secretKey);
            byte[] ivArray = Encoding.UTF8.GetBytes(iv);
            byte[] fromEncryptArray = Convert.FromBase64String(content);

            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = keyArray;
                aesAlg.IV = ivArray;

                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                using (MemoryStream msDecrypt = new MemoryStream(fromEncryptArray))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            return srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
        }

        public static string Hash256Encrypt(this string content)
        {
            using (HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secretKey)))
            {
                byte[] hashBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(content));

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < hashBytes.Length; i++)
                {
                    sb.Append(hashBytes[i].ToString("x2"));
                }
                return sb.ToString();
            }
        }

        public static string Hash512Encrypt(this string content)
        {
            byte[] originalBytes = Encoding.UTF8.GetBytes(content);

            using (SHA512 sha512 = SHA512.Create())
            {
                byte[] hashBytes = sha512.ComputeHash(originalBytes);

                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < hashBytes.Length; i++)
                {
                    stringBuilder.Append(hashBytes[i].ToString("x2"));
                }

                string hash = stringBuilder.ToString();
                return hash.ToString();
            }
        }

        public static string Hmacsha256HashEncrypt(this string rawData)
        {
            using (HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secretKey)))
            {
                byte[] hashValue = hmac.ComputeHash(Encoding.UTF8.GetBytes(rawData));

                StringBuilder hex = new StringBuilder(hashValue.Length * 2);
                foreach (byte b in hashValue)
                {
                    hex.AppendFormat("{0:x2}", b);
                }

                return hex.ToString();
            }
        }

        public static string Hmacsha512HashEncrypt(this string rawData)
        {
            byte[] keyByte = Encoding.UTF8.GetBytes(secretKey);
            byte[] messageBytes = Encoding.UTF8.GetBytes(rawData);

            using (HMACSHA512 hmac = new HMACSHA512(keyByte))
            {
                byte[] hexValue = hmac.ComputeHash(messageBytes);

                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < hexValue.Length; i++)
                {
                    string hexPart = hexValue[i].ToString("x2");
                    if (i % 2 == 0)
                        hexPart = hexPart.ToUpper();

                    builder.Append(hexPart);
                }

                return builder.ToString();
            }
        }

    }
}
